package com.example.summary.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wzn on 2019/9/11.
 */
public class LeetCode3 {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abba"));
    }

    public static int lengthOfLongestSubstring(String s) {
        int max = 0;
        Map<Character, Integer> xiaBiao = new HashMap<>();

        for (int i = 0, j = 0; i < s.length(); i++) {
            if (xiaBiao.containsKey(s.charAt(i))) {
                j = Math.max(xiaBiao.get(s.charAt(i)), j);
            }

            max = Math.max(max, i - j + 1);
            xiaBiao.put(s.charAt(i), i + 1);
        }

        return max;
    }
}
